package com.shujia.util

import java.io.File

import com.shujia.exception.TourException
import org.apache.spark.{SparkConf, SparkContext}
import org.slf4j.{Logger, LoggerFactory}

abstract class SparkTool {
  var LOGGER: Logger = _

  var sc: SparkContext = _
  var conf: SparkConf = _
  var day_id: String = _

  def main(args: Array[String]): Unit = {
    //构建打日志对象
    LOGGER = LoggerFactory.getLogger(this.getClass)


    if (args.length == 0) {
      LOGGER.error("请指定时间参数,比如 20180503")
      throw new TourException("请指定时间参数,比如 20180503")
    }

    day_id = args(0)

    conf = new SparkConf()
      .setAppName(this.getClass.getSimpleName)


    val flag = Config.getBoolean("is.local")


    if (!flag) {
      //增加依赖包
      val homePath = System.getProperty("user.dir")
      val homeFile = new File(homePath)
      val libPath = homeFile.getParentFile.getPath + "/lib"
      LOGGER.info("集群运行增加依赖包：依赖包路径为：" + libPath)
      val file = new File(libPath)
      val jars = file.listFiles
      val jarsList = jars.toList.map(_.getPath)

      conf.setJars(jarsList)

    }

    if (flag) {
      conf.setMaster("local[8]")
    }

    init()

    sc = new SparkContext(conf)

    run(args)

  }


  /**
    * 编写spark业务逻辑
    *
    */

  def run(args: Array[String])

  /**
    * 初始化方法，在子类设置spark运行时需要的参数
    * conf.setMaster("local")
    */
  def init()
}
